package com.wswy.wzcx.ui.dmv;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.View.OnClickListener;
import com.che.libcommon.ui.adapter.OnItemClickListener;
import com.wswy.wzcx.R;
import com.wswy.wzcx.model.AreaEntity;
import com.wswy.wzcx.model.dmv.DepartmentInfo;
import com.wswy.wzcx.model.dmv.ServiceInfoModel;
import com.wswy.wzcx.ui.activity.SampleBaseActivity;
import com.wswy.wzcx.ui.dmv.PopupChooser.OnPopupSelectedCallback;
import com.wswy.wzcx.widget.ArrowButton;
import java.util.List;

/**
 * 车管所
 */
public class CarDepartmentActivity extends SampleBaseActivity implements ICarDepartmentView{


  private PopupChooser popupChooser;

  private ArrowButton btnCity;

  private ArrowButton btnService;

  private RecyclerView recyclerView;

  private DepartmentAdapter adapter;

  private CarDepartmentPresenter mPresenter;

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_car_department);

    setAppTitle("车管所");

    btnCity = findViewById(R.id.arrow_city);
    btnService = findViewById(R.id.arrow_service);
    recyclerView = findViewById(R.id.recycler_view);


    btnCity.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        onBtnsClick(v);
      }
    });

    btnService.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        onBtnsClick(v);
      }
    });


    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));

    mPresenter = new CarDepartmentPresenter(this,this);

    popupChooser = new PopupChooser(this);
    popupChooser.popupSelectedCallback = new OnPopupSelectedCallback() {
      @Override
      public void onSelected(AreaEntity areaEntity, ServiceInfoModel serviceInfoModel) {
        mPresenter.load(areaEntity, serviceInfoModel);
        showLoading(true);

        if(adapter == null){
          adapter=new DepartmentAdapter(mPresenter.getPagination());


          adapter.setOnItemClickListener(new OnItemClickListener<DepartmentInfo>() {
            @Override
            public void onItemClick(View itemView, int position, DepartmentInfo departmentInfo) {

              CarDepartmentDetailActivity.start(CarDepartmentActivity.this,departmentInfo);
            }
          });

          recyclerView.setAdapter(adapter);
        }

      }
    };

    popupChooser.firstLoad(this);
  }

  @Override
  protected int getRootLayoutViewId() {
    return R.id.layout_root;
  }

  @Override
  protected int getLoadingFrameId() {
    return R.id.layout_content;
  }

  @Override
  protected boolean needLoading() {
    return true;
  }

  private void onBtnsClick(View view){

    if(view == btnCity){

      popupChooser.showCity(view);

    }else if(view == btnService){

      popupChooser.showService(view);

    }

  }


  @Override
  public void onChangeCity(String city) {
    btnCity.setText(city);

    btnCity.setSelected(false);

  }

  @Override
  public void onChangeService(String name) {

    btnService.setText(name);

    btnService.setSelected(false);
  }

  @Override
  public void openPopup(int resId) {
    if(resId  == R.id.arrow_city){
      btnCity.performClick();
    }else if(resId == R.id.arrow_service){
      btnService.performClick();
    }
  }

  @Override
  public void showResult(List<DepartmentInfo> list, boolean isFirst) {
    showData();

    if(isFirst){
      adapter.setNewData(list);
    }else {
      adapter.addData(list);
    }

    adapter.notifyDataSetChanged();

    if(isFirst && adapter.getItemCount() > 0){
      recyclerView.scrollToPosition(0);
    }

  }


  @Override
  protected void onDestroy() {
    super.onDestroy();
    if(mPresenter != null){
      mPresenter.onDestroy();
    }
  }
}
